*------------------------------------------------------------------------------------------------------------------------------
* Recode political knowledge - code by Silvia Keeling
*------------------------------------------------------------------------------------------------------------------------------
///////////////////////
///   QUESTION 1    ///
///////////////////////

replace W11_Q082 = upper(W11_Q082)
gen dom1 = W11_Q082

replace dom1 = subinstr(dom1, "CAAAAAAAMERE","CAMERE", .)
replace dom1 = subinstr(dom1, "CAMEREA","CAMERA", .)
replace dom1 = subinstr(dom1, "CANMERA","CAMERA", .)
replace dom1 = subinstr(dom1, "MAMERA","CAMERA", .)
replace dom1 = subinstr(dom1, "SEATORI","SENATORI", .)
replace dom1 = subinstr(dom1, "SANATORI","SENATORI", .)
replace dom1 = subinstr(dom1, "SENAATORI","SENATORI", .)
replace dom1 = subinstr(dom1, "PARLEMENTO","PARLAMENTO", .)
replace dom1 = subinstr(dom1, "PERLAMENTO","PARLAMENTO", .)
replace dom1 = subinstr(dom1, "PARALAMENTO","PARLAMENTO", .)
replace dom1 = subinstr(dom1, "DEOUTATI", "DEPUTATI", .)
replace dom1 = subinstr(dom1, "CAMERA DEI SENATORI", "SENATO", .)
replace dom1 = subinstr(dom1, "REGINI", "REGIONI", .)
replace dom1 = subinstr(dom1, "REGONI", "REGIONI", .) 


screening, source(dom1, upper trim itrim removesign) ///
key("grandi elettori") letters (11) new (varr, numeric) recode (1 "10") explore(tab)
recode varr (. = 0 "0"), gen (varr1) 

screening, source(dom1, upper trim itrim removesign) ///
key("rappresentanti" "delegati" "presidenti" "regioni") letters (9 6 5 5) ///
 new (var0, numeric) recode (1,4 "10" 2,4 "10" 3,4 "0" 4 "10" ) explore(count)
recode var0 (.=0 "0"), gen (var00) 

screening, source(dom1, upper trim itrim removesign) ///
key("parlamento" "camere" "seduta" "regioni" "unite" "congiunte" "grandi elettori") letters (5 6 5 5 5 5 10) new (var1, numeric) ///
recode (1,3,4"5" 2,3,4"5" 2,5"4" 2,6,"4" 1,7"4" 1,3"4" 1,4"4" 1,5"4" 1,6"4" 2,3"4" 2,4"4" 2,7"4" 1"2" 2"2" 4"2" 3"1" 5"1" 6"3" 7"3") explore(count)
recode var1 (.=0 "0"), gen (var11) 

screening, source(dom1,upper trim itrim removesign) ///
key("parlamento" "camera" "senato" "deputati" "seduta" "regioni" "unite" "camere") letters (6 6 5 5 5 4 3 6) new (var2, numeric) ///
recode( 2,3,5,6"5" 3,4,5,6"5" 1,7"4" 2,3,5"4" 2,3,7 "4" 3,4,6"4" 3,4,5"4" 2,3,4"2" 1,2,3"0" 8,3,4"0" 1,3,4,"2" 2,3"2" 3,4"2" 1,3"1" 1"0" 2"1" 3"1" 4"1" 5"1" 6"1" 7"0" 8"0") explore(count)
recode var2 (.=0 "0"), gen (var22) 

gen var3 = varr1 + var00 + var11 + var22 if W11_Q007 != .

recode var3 (0 1 3 13 = 0 "sbagliato")(else = 1 "giusto") if W11_Q007 != ., gen (var33)
rename var33 know_1
tab know_1

drop dom1 - var3

///////////////////////
///   QUESTION 2    ///
///////////////////////

replace W11_Q084 = upper(W11_Q084)
gen dom2 = W11_Q084

replace dom2 = subinstr(dom2, "BLODRINI", "BOLDRINI", .)
replace dom2 = subinstr(dom2, "BODRINI", "BOLDRINI", .)
replace dom2 = subinstr(dom2, "BOLDINI", "BOLDRINI", .)
replace dom2 = subinstr(dom2, "BOLFRINI", "BOLDRINI", .)
replace dom2 = subinstr(dom2, "BOLSRINI", "BOLDRINI", .)
replace dom2 = subinstr(dom2, "BOLTRINI", "BOLDRINI", .)
replace dom2 = subinstr(dom2, "BOLBRINI", "BOLDRINI", .)
replace dom2 = subinstr(dom2, "BOLRINI", "BOLDRINI", .)
replace dom2 = subinstr(dom2, "BOLRDINI", "BOLDRINI", .)
replace dom2 = subinstr(dom2, "BOLBRINI", "BOLDRINI", .)
replace dom2 = subinstr(dom2, "POLDRINI", "BOLDRINI", .)


screening, source(dom2, upper trim itrim removesign) ///
key("BOLDRINI") letters (5) new (dom2_0, numeric) recode (1 "10") explore(tab)
recode dom2_0 (. = 0 "0"), gen (dom20) 

screening, source(dom2, upper trim itrim removesign) ///
key("DI MAIO") letters (6) new (dom2_1, numeric) recode (1 "1") explore(tab)
recode dom2_1 (. = 0 "0"), gen (dom21) 

screening, source(dom2, upper trim itrim removesign) ///
key("GRASSO") letters (5) new (dom2_2, numeric) recode (1 "1") explore(tab)
recode dom2_2 (. = 0 "0"), gen (dom22) 

screening, source(dom2, upper trim itrim removesign) ///
key("GIACCHETTI") letters (5) new (dom2_3, numeric) recode (1 "1") explore(tab)
recode dom2_3 (. = 0 "0"), gen (dom23) 

gen variabile = dom20 + dom21 + dom22 + dom23 if W11_Q007 != .
tab variabile
recode variabile(10 = 1 "giusto") (else = 0 "sbagliato") if W11_Q007 != ., gen (know_2) 
tab know_2

drop dom2 - variabile

///////////////////////
///   QUESTION 3    ///
///////////////////////


destring W11_Q086, gen(dom3)
*gen know_3 = 0
gen know_3 = .
replace know_3 = 0 if dom3 < 600 | dom3 > 600
replace know_3 = 0.5 if dom3 >= 600 & dom3 <= 650
replace know_3 = 1 if dom3 == 630

*------------------------------------------------------------------------------------------------------------------------------
* Polychoric Cronbach's alpha (see: https://www.stata.com/statalist/archive/2012-02/msg00696.html)

* requires external package "polychoric" - run the following line to download it:
* findit polychoric

polychoric know_*
local nobs = r(N)
matrix define C = r(R)
factormat C, n(`nobs') factors(1)

tempname L Psi
matrix define `L' = e(L)
matrix define `Psi' = e(Psi)

local p = rowsof(`L')

tempname f f2 u2
scalar define `f' = 0
scalar define `f2' = 0
scalar define `u2' = 0
forvalues i = 1/`p' {
scalar define `f' = `f' + `L'[`i', 1]
scalar define `f2' = `f2' + `L'[`i', 1] * `L'[`i', 1]
scalar define `u2' = `u2' + `Psi'[1, `i']
}
scalar define `f' = `f' / `p'
scalar define `f2' = `f2' / `p'
scalar define `u2' = `u2' / `p'

tempname pf2
scalar define `pf2' = `p' * `f' * `f'
scalar define alpha = `p' / (`p' - 1) * ///
(`pf2' - `f2') / (`pf2' + `u2')

display in smcl as text "Ordinal alpha = " as result %06.4f alpha

*------------------------------------------------------------------------------------------------------------------------------
* IRT
irt grm know_1 know_2 know_3
predict polknow_irt, latent
label variable polknow_irt "Political Knowledge (IRT)"


